<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/3/2
 * Time: 下午8:22
 */
namespace Home\Controller;
use Common\Controller\HomeBaseController;
use Lib\captcha;

class GoodsController extends HomeBaseController
{
    public function detail()
    {
        $id = I('get.id',0);
        if(empty($id)){
            $this->error('非法操作！');
        }
        $this_goods = D('goods')->get_one_info(['id'=>$id],'id,goods_name,bid,class_id,litimg,market_price,fd_price,number,description,body');
        $p_info = D('classification')->get_all_p($this_goods['class_id']);
        dump($this_goods);
        $this->assign('this_goods',$this_goods);
        $this->assign('p_info',$p_info);
        $this->display();
    }

    public function change_number()
    {
        if(IS_GET){
            $id = I('get.goods_id',0);
            if($id){
                $this->ajaxReturn(false);
            }
            $act = I('get.act','');
            if($act == 'get_products_info'){
                $products_info = D('goods')->get_one_info(['id'=>$id]);
                $this->ajaxReturn(['product_number'=>$products_info['number']]);
            }
        }
    }

    public function change_price()
    {
        if(IS_GET){
            $id = I('get.id',0);
            if(empty($id)){
                $this->ajaxReturn(['err_msg'=>'非法操作！']);
            }
            $number = I('get.number',0);
            if(empty($number)){
                $this->ajaxReturn(['err_msg'=>'非法操作！']);
            }
            $fd_price = D('goods')->where(['id'=>$id])->getField('fd_price');
            if(empty($fd_price)){
                $this->ajaxReturn(['err_msg'=>'获取价格失败！']);
            }
            $result = $fd_price*$number;
            $this->ajaxReturn(['err_msg'=>'','result'=>$result]);
        }
    }

    public function captcha()
    {
        define('IN_ECS', true);
        define('INIT_NO_SMARTY', true);
        $img = new captcha('./Include/captcha/');
        @ob_end_clean(); //清除之前出现的多余输入
        if (!empty($_SESSION))
        {
            $img->session_word = 'captcha_login';
        }
        $img->generate_image();
    }
}